//
//  CommitInfoViewController.m
//  carcareIOS
//
//  Created by wr on 16/1/12.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "CommitInfoViewController.h"

#import "PictureMgr.h"

#import "UploadView.h"

#import "UploadStatusView.h"

//#import "BaseWebViewController.h"

#define SECTION_HEIGHT (16)
#define CELL_HEIGHT (159)

typedef NS_ENUM(NSInteger , CELL_LABLE_TYPE)
{
    CELL_LABLE_CENTER_TAG = 10,
    CELL_IMAGE_VIEW_TAG = 30,
};

@interface CommitInfoViewController ()

@property (nonatomic , readonly) NSArray * titleArray;

@property (nonatomic , assign) NSInteger selectIndex;

@property (nonatomic , strong) UploadView * uploadView;

@property (nonatomic , strong) UploadStatusView * stateView;

@end

@implementation CommitInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"拍照验证";
    
    __weak __typeof(self) wself = self;
    [self navSetupLeftTitle:@"取消" action:^{
        
        [wself dismissViewControllerAnimated:YES completion:nil];
        
    }];
    self.uploadView = [[UploadView alloc]initWithUploadLicenseName:[self.paramsDic objectForKey:@"img_describe"] andDescripeDetail:[NSString stringWithFormat:@"点击上传%@",[self.paramsDic objectForKey:@"img_title"]] addImageName:@"insuUploadPhotoImage" uploadBtnTitle:@"上传"];
    self.uploadView.addbuttonClick = ^(UIButton * sender)
    {
        [PICTUREMGR selectPictureWithController:wself callBack:^(UIImage * backImage) {
            wself.uploadView.upImage = backImage;
        }];
    };
    
    //uploadErrorImage  uploadingImage  uploadSuccessImage
    self.stateView = [[UploadStatusView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH - 60 * __MAIN_RATIO_375 * 2, 138 * __MAIN_RATIO_375) withImageName:@"uploadingImage" withTitle:@"正在上传..."];
    
    self.uploadView.commitbuttonClick = ^ (UIButton * sender)
    {
    
        [AlertMgr showView:wself.stateView];
        [PICTUREMGR uploadPictureType:wself.paramsDic[@"icz_imgType"] withNetSuccess:^(id JSON) {
            [wself.stateView changeImageName:@"uploadSuccessImage" withTitle:@"上传成功"];
            [NSTimer scheduledTimerWithTimeInterval:1 block:^{
                [AlertMgr dismiss:TYPE_VIEW_ALERT];
                
                // 在这里进行界面跳转 ，跳转的url
                [wself dismissViewControllerAnimated:YES completion:^{
                    if (wself.UploadSuccess) {
                        wself.UploadSuccess(JSON[@"info"]);
                    }
                }];
                
            } repeats:NO];
            
        } fail:^(id JSON) {
            [wself.stateView changeImageName:@"uploadErrorImage" withTitle:@"上传失败，请重新上传"];
            [NSTimer scheduledTimerWithTimeInterval:3 block:^{
                [AlertMgr dismiss:TYPE_VIEW_ALERT];
                
            } repeats:NO];
            
        }];
        
        [NSTimer scheduledTimerWithTimeInterval:10 block:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
            
        } repeats:NO];
        
    };
    [self.view addSubview:self.uploadView];
    
    
    [self resetTableView];
}

- (NSArray *)titleArray
{
    return @[@[@"空白"],@[@"上传您的行驶证正面",@"上传您的行驶证反面",@"上传您的身份证正面",@"上传您的身份证正面",@"其他证件1"]];
}
- (void)resetTableView
{
    
    
    [self.tableView setFrame:CGRectMake(0, 0 , __MAIN_WIDTH, 0)];
    self.tableView.backgroundColor = [Theme backGroundColor];
    
    
}
#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.section == 1 ? (CELL_HEIGHT + SECTION_HEIGHT) : SECTION_HEIGHT);
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    switch (indexPath.section) {
        case 0:
        {
            NSString * cellID = @"NOCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            cell.backgroundColor = [Theme backGroundColor];
            
            [[[UIView viewWithFrame:CGRectMake(0, 0 , __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineNewColor]] setupOnView:cell];
            [[[UIView viewWithFrame:CGRectMake(0, SECTION_HEIGHT - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineNewColor]] setupOnView:cell];
        }
            break;
        case 1:
        {
            static NSString * cellID = @"cellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                CGFloat backViewX = 22 * __MAIN_RATIO_375;
                CGFloat backViewW = __MAIN_WIDTH - backViewX * 2;
                UIView * backView = [[[UIView viewWithFrame:CGRectMake(backViewX, SECTION_HEIGHT, backViewW, CELL_HEIGHT)] resetBackgroundColor:[Theme backGroundColor]] setupOnView:cell];
                backView.layer.cornerRadius = 4;
                backView.layer.masksToBounds = YES;
                backView.layer.borderColor = [Theme lineColor].CGColor;
                backView.layer.borderWidth = 1;
                
                UIImage * instureAddImage = ImageNamed(@"insuUploadPhotoImage");
                CGFloat imageCenterY = backView.height / 3;
                [Factory imageViewWithCenter:CGPointMake(backView.width / 2, imageCenterY) image:instureAddImage onView:backView];
                
                CGFloat labelH = 20;
                CGFloat labelX = 10 * __MAIN_RATIO_375;
                UILabel * centerLabel = [Factory labelWithFrame:CGRectMake(labelX , imageCenterY + instureAddImage.size.height / 2 + (backView.height - (imageCenterY + instureAddImage.size.height / 2 + labelH))/ 2, backView.width - labelX * 2, labelH) font:FONT_DEFAULT_Light(15) text:@"" textColor:COLOR_HEXSTRING(@"#878787") onView:backView textAlignment:NSTextAlignmentCenter];
                centerLabel.tag = CELL_LABLE_CENTER_TAG + indexPath.row;
                
                CGFloat infoImageVX = 45 * __MAIN_RATIO_375;
                UIImageView * infoImageV = [Factory imageViewWithFrame:CGRectMake(infoImageVX, 5, backView.width - infoImageVX * 2, backView.height - 5 * 2) image:nil onView:backView];
                infoImageV.backgroundColor = [UIColor redColor];
                infoImageV.tag = CELL_IMAGE_VIEW_TAG + indexPath.row;
                
            }
            UILabel * centerLabel = (UILabel *)[cell viewWithTag:CELL_LABLE_CENTER_TAG + indexPath.row];
            centerLabel.text = [NSString stringWithFormat:@"%@",self.titleArray[indexPath.section][indexPath.row]];
        }
            break;
        default:
            break;
    }
    

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView endEditing:YES];
    
    if (![Check isVisiter]) {
        __weak __typeof(self) wself = self;
        self.selectIndex = indexPath.row;
        [PICTUREMGR selectPictureWithController:self callBack:^(UIImage * backImage) {
            UITableViewCell * cell = [wself.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:wself.selectIndex inSection:1]];
                UIImageView * imageView = (UIImageView *)[cell viewWithTag:CELL_IMAGE_VIEW_TAG + wself.selectIndex];
                [imageView setImage:backImage];
            [PICTUREMGR uploadPictureType:@"XSZ" withNetSuccess:^(id JSON) {
                
            } fail:^(id JSON) {
                
            }];
        }];
//        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:LOCSTRING(@"cancel") destructiveButtonTitle:nil otherButtonTitles:LOCSTRING(@"menu_camera"),LOCSTRING(@"menu_pic"), nil];
//        [actionSheet showInView:self.tableView];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
